package com.weapons.week0903_weapons.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.constant.UrlString;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;


/**
 * A simple {@link Fragment} subclass.
 */
public class RoundFragment extends Fragment {
    private SwipeRefreshLayout srfreshLayout;
    private TabLayout mTlCount;
    private ViewPager mVpCount;
    private Activity mActivity;
    private String mpos = "";
    private List<Fragment> mList;

    private String[] mTitle={"热点","关注","焦点","防务"};

    public RoundFragment newInstance(String pos) {
        RoundFragment fragment = new RoundFragment();
        Bundle bundle = new Bundle();
        bundle.putString("pos", pos);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        Bundle bundle = getArguments();
        if (bundle != null) {
            mpos = bundle.getString("pos");
        }

        Log.i("pos", "pas++++++" + mpos);


    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_round, container, false);
        //srfreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.image_swiperefresh);
        mTlCount= (TabLayout) view.findViewById(R.id.tl_round);
        mVpCount= (ViewPager) view.findViewById(R.id.vp_round);
        initDatas();
        MyVpAdapter adapter=new MyVpAdapter(getChildFragmentManager(),mList);
        mVpCount.setAdapter(adapter);
        mVpCount.setOffscreenPageLimit(4);
        mTlCount.setupWithViewPager(mVpCount);

        int tabCount = mTlCount.getTabCount();
        for (int i = 0; i < tabCount; i++) {
            TabLayout.Tab tab = mTlCount.getTabAt(i);
            tab.setText(mTitle[i]);
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initDatas() {
        mList=new ArrayList<>();
        if (getArguments() != null) {
            mList.add(new RChildFragment().newInstance(UrlString.URL_ROUND_NEWS ));
            mList.add(new RChildFragment().newInstance(UrlString.URL_ROUND_ATTENTION));
            mList.add(new RChildFragment().newInstance(UrlString.URL_ROUND_FOCUS));
            mList.add(new RChildFragment().newInstance(UrlString.URL_ROUND_DEFENSE));
        }
        mVpCount.setCurrentItem(0);

        Log.i("pos", String.valueOf(mList.size()));

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    class MyVpAdapter extends FragmentPagerAdapter {
        private FragmentManager fm;
        private List<Fragment> mFragmnets;
        public MyVpAdapter(FragmentManager fm,List<Fragment> fragments) {
            super(fm);
            this.fm=fm;
            this.mFragmnets=fragments;
        }

        @Override
        public Fragment getItem(int position) {

            return mFragmnets.get(position);
        }

        @Override
        public int getCount() {
            Log.i("pos","RoundFragment"+mFragmnets.size());
            return mFragmnets.size();
        }
    }

}
